<template>
  <div>
    <!--查询表单开始-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="searchObj.hosname" placeholder="医院名称" />
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchObj.hoscode" placeholder="医院编号" />
      </el-form-item>
      <el-button
        type="primary"
        icon="el-icon-search"
        @click="getHospitalSetByPageByCondition()"
        >查询</el-button
      >
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>
    <!-- 查询表单结束 -->

    <!-- 遍历数据项 table开始 -->
    <!-- 工具条 -->
    <div>
      <el-button type="danger" size="mini" @click="removeRows()"
        >批量删除</el-button
      >
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="数据加载中"
      border
      fit
      highlight-current-row
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />

      <el-table-column label="序号" width="70" align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="hosname" label="医院名称" width="180" />

      <el-table-column prop="hoscode" label="医院编号" width="160" />

      <el-table-column prop="apiUrl" label="地址" width="200" />

      <el-table-column prop="contactsName" label="联系人" />

      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          {{ scope.row.status === 1 ? "可用" : "不可用" }}
        </template>
      </el-table-column>

      <el-table-column label="操作" width="400" align="center">
        <template slot-scope="scope">
          <router-link :to="'/yygh/hospitalSet/edit/' + scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit"
              >编辑</el-button
            >
          </router-link>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="deleteHospitalSetById(scope.row.id)"
            >删除</el-button
          >
          <el-button
            v-if="scope.row.status == 1"
            type="primary"
            size="mini"
            icon="el-icon-delete"
            @click="lockOrUnlock(scope.row.id, 0)"
            >锁定</el-button
          >
          <el-button
            v-if="scope.row.status == 0"
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="lockOrUnlock(scope.row.id, 1)"
            >取消锁定</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- table结束 -->
    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center"
      layout="total, prev, pager, next, jumper"
      @current-change="getHospitalSetByPageByCondition"
    />
  </div>
</template>
<script>
import hospitalSetApi from "@/api/hospitalSet";
export default {
  data() {
    return {
      listLoading: true, //是否显示loading信息
      list: [], //数据列表
      total: 0, //总记录数
      page: 1, //当前页码
      limit: 2, //每页记录数
      searchObj: {}, //查询条件
      multipleSelection: [], // 批量选择中选择的记录列表
    };
  },
  created() {
    this.getHospitalSetByPageByCondition();
  },
  methods: {
    //带条件带分页查询医院设置
    getHospitalSetByPageByCondition(page = 1) {
      this.page = page;
      //显示正在加载
      this.listLoading = true;
      hospitalSetApi
        .getHospitalSetByPageByCondition(this.page, this.limit, this.searchObj)
        .then((response) => {
          this.list = response.data.rows;
          this.total = response.data.total;
          //不显示正在加载
          this.listLoading = false;
        });
    },
    //根据id删除医院设置
    deleteHospitalSetById(id) {
      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return hospitalSetApi.deleteHospitalSetById(id);
        })
        .then(() => {
          this.getHospitalSetByPageByCondition();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch((response) => {
          // 失败
          if (response === "cancel") {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          } else {
            this.$message({
              type: "error",
              message: "删除失败",
            });
          }
        });
    },
    // 当表格复选框选项发生变化的时候触发
    handleSelectionChange(selection) {
      this.multipleSelection = selection;
    },
    //批量删除医院设置
    removeRows() {
      this.$confirm("此操作将永久删除医院是设置信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        //确定执行then方法
        var ids = [];
        //遍历数组得到每个id值，设置到ids里面
        for (var i = 0; i < this.multipleSelection.length; i++) {
          var obj = this.multipleSelection[i];
          var id = obj.id;
          ids.push(id);
        }
        //调用接口
        hospitalSetApi.removeRows(ids).then((response) => {
          //提示
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          //刷新页面
          this.getHospitalSetByPageByCondition(1);
        });
      });
    },
    //上锁或解锁
    lockOrUnlock(id,status){
      hospitalSetApi.lockOrUnlock(id,status)
      .then(response=>{
        this.getHospitalSetByPageByCondition()
      })
    }
  },
};
</script>